package com.cz.video.web.controller.website;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.collection.CollUtil;
import com.cz.video.common.annotation.LegalValidate;
import com.cz.video.common.annotation.Log;
import com.cz.video.common.core.controller.BaseController;
import com.cz.video.common.core.domain.PageQuery;
import com.cz.video.common.core.domain.R;
import com.cz.video.common.core.page.TableDataInfo;
import com.cz.video.common.enums.BusinessType;
import com.cz.video.common.exception.ServiceException;
import com.cz.video.common.helper.LoginHelper;
import com.cz.video.system.service.ISysUserService;
import com.cz.video.website.domain.WebsiteComment;
import com.cz.video.website.domain.bo.WebsiteCommentBo;
import com.cz.video.website.domain.vo.WebsiteCommentVo;
import com.cz.video.website.service.IWebsiteCommentService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 评论表 前端控制器
 * </p>
 *
 * @author LLF
 * @since 2023-10-16
 */

//查询评论的时候关联用户表和评论表，把用户头像和ID查出来
@RestController
@RequiredArgsConstructor
@RequestMapping("/website-comment")
public class WebsiteCommentController extends BaseController {

    private final IWebsiteCommentService commentService;
    private final ISysUserService userService;

    @Operation(summary = "分页查询评论")
    @GetMapping("/page")
    public TableDataInfo<WebsiteCommentVo> page(WebsiteCommentBo websiteComment,PageQuery pageQuery){
        return commentService.selectPageCommentList( websiteComment,pageQuery);
    }

    @Operation(summary = "添加评论表")
    @Log(title = "添加评论表", businessType = BusinessType.INSERT)//自定义的日志注解，记录添加评论的操作
    @PostMapping("")
    @LegalValidate
    public R<Void> add(@RequestBody @Validated WebsiteCommentBo websiteComment){//@RequestBody后台获取客户端请求的数据
        websiteComment.setUserId(LoginHelper.getUserId());//获取ID，设置评论ID
        return toAjax(commentService.insertComment( websiteComment));
    }

    /**
     * 删除评论表
     * @param id
     * @return
     */
//    @SaCheckPermission("website:Comment:delete")
    @Operation(summary = "删除评论表")
    @Log(title = "删除评论表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public R<Void> delete(@PathVariable Long id){
        int result = commentService.deleteById(id);
        if (result > 0) {
            return toAjax(result);
        } else {
            throw new ServiceException("删除评论失败，该评论不存在。");
        }
    }

//    /**
//     * 修改评论表
//     */
//    @SaCheckPermission("website:Comment:edit")//检查可修改权限
//    @Log(title = "修改评论表", businessType = BusinessType.UPDATE)
//    @PutMapping("")
//    public R<Void> edit(@Validated @RequestBody WebsiteCommentBo websiteComment, SysUser user) {
//        userService.checkUserAllowed(user);
//        userService.checkUserDataScope(user.getUserId());
//        return toAjax(commentService.updateComment(websiteComment));
//    }

    /**
     * 分页查询评论树
     *
     * @param commentBo 查询参数
     * @param pageQuery 分页参数
     *
     * @return 分页评论树信息
     */
    @SaIgnore
    @Operation(summary = "分页查询评论树")
    @GetMapping("/treeselect")//HTTP GET请求映射到路径"/treeselect"，触发方法。
    public TableDataInfo<?> treeselect(WebsiteCommentBo commentBo, PageQuery pageQuery) {
        //先分页查询父评论
        commentBo.setParentId(0L);//先设置ParentId为0
        TableDataInfo<?> commentPage = commentService.listParent(commentBo, pageQuery);
        List<WebsiteCommentVo> commentList = (List<WebsiteCommentVo>) commentPage.getRows();
        //再查询对应的子评论，只会查询点赞最高的前两个
        if (CollUtil.isNotEmpty(commentList)) {
            //查询对应的子评论，只会查询点赞最高的前两个
            List<WebsiteCommentVo> subCommentList = commentService.getReplyList(
                commentList.stream().map(WebsiteComment::getCommentId).collect(Collectors.toList()));
            commentList.addAll(subCommentList);
        }
        List trees = commentService.buildCommentTreeSelect(commentList);
        commentPage.setRows(trees);
        return commentPage;
    }
}
